﻿using System;
using System.Activities.Hosting;
using System.Activities.Persistence;
using System.Activities.Tracking;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuditSample.Persistence
{
    public class HistoryTrackingParticipant_Old : TrackingParticipant, IWorkflowInstanceExtension
    {
        
        PersistenceIOParticipant persistenceParticipant;

        public HistoryTrackingParticipant_Old()
        {
            persistenceParticipant = new HistorySavePersistenceParticipant(this);
        }

        public void Save()
        {
            //dbContext.WorkFlowHistories.Add(this.workFlowHistory);
            //dbContext.SaveChanges();
        }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
            //get the custom record
            if (record != null)
            {
                if (record is CustomTrackingRecord)
                {
                    CustomTrackingRecord customRecord = (CustomTrackingRecord)record;
                    if (customRecord.Name.Equals("ActionExecuted", StringComparison.InvariantCultureIgnoreCase))
                    {
                        using (Model1 dbContext = new Model1()) { 
                            WorkFlowHistory workFlowHistory = new WorkFlowHistory();
                            WorkFlowInstance workflowInstance = dbContext.WorkFlowInstances.FirstOrDefault(x => x.Id == record.InstanceId.ToString());
                            if (workflowInstance == null) return;
                            workFlowHistory.Id = Guid.NewGuid().ToString();
                            workFlowHistory.InstanceId = record.InstanceId.ToString();
                            workFlowHistory.RecordNumber = record.RecordNumber.ToString();
                            workFlowHistory.State = customRecord.Data.GetString("State");
                            workFlowHistory.Action = customRecord.Data.GetString("Action");
                            workFlowHistory.Auditer = customRecord.Data.GetString("Auditer");
                            workFlowHistory.Comment = customRecord.Data.GetString("Comment");
                            workFlowHistory.StartDate = workflowInstance.UpdatedDate;
                            workFlowHistory.EndDate = customRecord.Data.GetDate("Date");
                            dbContext.WorkFlowHistories.Add(workFlowHistory);
                            dbContext.SaveChanges();
                        }
                    }
                }
            }
           
        }

        

        IEnumerable<object> IWorkflowInstanceExtension.GetAdditionalExtensions()
        {
            return new object[] { this.persistenceParticipant };
        }

        public void SetInstance(WorkflowInstanceProxy instance)
        {
        }

        class HistorySavePersistenceParticipant : PersistenceIOParticipant
        {
            HistoryTrackingParticipant_Old Participant { get; set; }

            public HistorySavePersistenceParticipant(HistoryTrackingParticipant_Old participant)
                : base(true, false)
            {
                this.Participant = participant;
            }


            protected override IAsyncResult BeginOnSave(IDictionary<System.Xml.Linq.XName, object> readWriteValues, IDictionary<System.Xml.Linq.XName, object> writeOnlyValues, TimeSpan timeout, AsyncCallback callback, object state)
            {
                this.Participant.Save();
                return base.BeginOnSave(readWriteValues, writeOnlyValues, timeout, callback, state);
            }

            protected override void EndOnSave(IAsyncResult result)
            {
            }

            protected override void Abort()
            {
            }
        }
    }
}
